﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using Microsoft.Win32;

namespace PipePrinter
{
    public class ComPortConfig
    {
        public string PortName;
        public string BaudRate;
        public string DataBits;
        public string Parity;
        public string StopBits;
        public string Handshake;

        public void ConfigLink(LinkLayer link)
        {
            System.IO.Ports.Parity p = (System.IO.Ports.Parity)System.Enum.Parse(typeof(System.IO.Ports.Parity), (string)Parity);
            System.IO.Ports.StopBits s = (System.IO.Ports.StopBits)System.Enum.Parse(typeof(System.IO.Ports.StopBits), (string)StopBits);
            System.IO.Ports.Handshake h = (System.IO.Ports.Handshake)System.Enum.Parse(typeof(System.IO.Ports.Handshake), (string)Handshake);

            link.SerialPortSetup(PortName,BaudRate, p, h, DataBits, s);
        }
    }

    public enum DelayType
    { 
        ByCount,
        ByTime,
    }

    public class DelayConfig
    {
        public DelayType Type;
        public string CountValue;
        public string DelayMsValue;
    }

    public class SysConfig
    {
        private static SysConfig instance = null;

        private RegistryKey cbSubKey;
        public ComPortConfig InputPort;
        public ComPortConfig OutputPort;
        public DelayConfig Delay;

        private static string RegistryPath = "Software\\PipePrinter";

        private static string KEY_IPORT = "iPort";
        private string KEY_IBAUD = "iBaud";
        private string KEY_IDATABITS = "iDataBits";
        private string KEY_IPARITY = "iParity";
        private string KEY_ISTOPBITS = "iStopBits";
        private string KEY_IHANDSHAKE = "iHandshake";

        private string KEY_OPORT = "oPort";
        private string KEY_OBAUD = "oBaud";
        private string KEY_ODATABITS = "oDataBits";
        private string KEY_OPARITY = "oParity";
        private string KEY_OSTOPBITS = "oStopBits";
        private string KEY_OHANDSHAKE = "oHandshake";

        private string KEY_DELAYTYPE = "DelayType";
        private string KEY_COUNTVALUE = "CountValue";
        private string KEY_DELAYMSVALUE = "DelayMsValue";


        public static SysConfig getInstance()
        {
            if (instance == null)
            {
                instance = new SysConfig();
            }
            return instance;
        }

        public static bool HasConfigured()
        {
            RegistryKey SubKey = Registry.LocalMachine.OpenSubKey(RegistryPath, true);
            if (SubKey == null)
                return false;
            else
            {
                if ( ((string)SubKey.GetValue(KEY_IPORT, "PORT?")).Equals("PORT?"))
                    return false;
                else
                    return true;
            }
        }

        private SysConfig()
        {
            try
            {
                InputPort = new ComPortConfig();
                OutputPort = new ComPortConfig();
                Delay = new DelayConfig();

                cbSubKey = Registry.LocalMachine.OpenSubKey(RegistryPath, true);
                if (cbSubKey == null)
                {
                    Registry.LocalMachine.CreateSubKey(RegistryPath);
                    cbSubKey = Registry.LocalMachine.OpenSubKey(RegistryPath, true);
                }

                GetInputPortConfig();
                GetOutputPortConfig();
                GetDelayConfig();
            }
            catch (Exception)
            {
            }
        }

        public void Save()
        {
            SaveInputPortConfig();
            SaveOutputPortConfig();
            SaveDelayConfig();
        }

        public ComPortConfig GetInputPortConfig()
        {
            InputPort.PortName = (string)cbSubKey.GetValue(KEY_IPORT, "COM1");
            InputPort.BaudRate = (string)cbSubKey.GetValue(KEY_IBAUD, "115200").ToString();
            InputPort.DataBits = (string)cbSubKey.GetValue(KEY_IDATABITS, "8").ToString();
            InputPort.Parity = (string)cbSubKey.GetValue(KEY_IPARITY, Parity.None.ToString());
            InputPort.Handshake = (string)cbSubKey.GetValue(KEY_IHANDSHAKE, Handshake.None.ToString());
            InputPort.StopBits = (string)cbSubKey.GetValue(KEY_ISTOPBITS, StopBits.One.ToString());

            return InputPort;
        }

        public void SaveInputPortConfig()
        {
            cbSubKey.SetValue(KEY_IPORT, InputPort.PortName);
            cbSubKey.SetValue(KEY_IBAUD, InputPort.BaudRate);
            cbSubKey.SetValue(KEY_IPARITY, InputPort.Parity);
            cbSubKey.SetValue(KEY_IHANDSHAKE, InputPort.Handshake);
            cbSubKey.SetValue(KEY_IDATABITS, InputPort.DataBits);
            cbSubKey.SetValue(KEY_ISTOPBITS, InputPort.StopBits);
        }

        public ComPortConfig GetOutputPortConfig()
        {
            OutputPort.PortName = (string)cbSubKey.GetValue(KEY_OPORT, "COM2");
            OutputPort.BaudRate = (string)cbSubKey.GetValue(KEY_OBAUD, "115200").ToString();
            OutputPort.DataBits = (string)cbSubKey.GetValue(KEY_ODATABITS, "8").ToString();
            OutputPort.Parity = (string)cbSubKey.GetValue(KEY_OPARITY, Parity.None.ToString());
            OutputPort.Handshake = (string)cbSubKey.GetValue(KEY_OHANDSHAKE, Handshake.None.ToString());
            OutputPort.StopBits = (string)cbSubKey.GetValue(KEY_OSTOPBITS, StopBits.One.ToString());

            return OutputPort;
        }

        public void SaveOutputPortConfig()
        {
            cbSubKey.SetValue(KEY_OPORT, OutputPort.PortName);
            cbSubKey.SetValue(KEY_OBAUD, OutputPort.BaudRate);
            cbSubKey.SetValue(KEY_OPARITY, OutputPort.Parity);
            cbSubKey.SetValue(KEY_OHANDSHAKE, OutputPort.Handshake);
            cbSubKey.SetValue(KEY_ODATABITS, OutputPort.DataBits);
            cbSubKey.SetValue(KEY_OSTOPBITS, OutputPort.StopBits);
        }

        public DelayConfig GetDelayConfig()
        {
            Delay.Type = (DelayType)System.Enum.Parse(typeof(DelayType), (string)cbSubKey.GetValue(KEY_DELAYTYPE, "ByCount"));
            Delay.CountValue = (string)cbSubKey.GetValue(KEY_COUNTVALUE, "12");
            Delay.DelayMsValue = (string)cbSubKey.GetValue(KEY_DELAYMSVALUE, "12000");

            return Delay;
        }

        public void SaveDelayConfig()
        {
            cbSubKey.SetValue(KEY_DELAYTYPE, Delay.Type);
            cbSubKey.SetValue(KEY_COUNTVALUE, Delay.CountValue);
            cbSubKey.SetValue(KEY_DELAYMSVALUE, Delay.DelayMsValue);
        }
    }


}
